package com.xrh.xdemo.service.service;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xrh.core.bean.CommonPage;
import com.xrh.core.bean.CommonResult;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.entity.MarkLibDTO;
import com.xrh.xdemo.service.aspects.OperateTraceAspect;
import com.xrh.xdemo.service.entity.MarkLibEntity;
import com.xrh.xdemo.api.request.addRequest.MarkLibAddRequest;
import com.xrh.xdemo.api.request.queryRequest.MarkLibQueryRequest;
import com.xrh.xdemo.api.request.updateRequest.MarkLibUpdateRequest;
import com.xrh.xdemo.service.mapper.MarkLibMapper;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class MarkLibService {

	@Resource
	MarkLibMapper markLibMapper;
	
	/**
	 * @function 新增MarkLib
	 * @param markLibAddRequest
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public Long addMarkLib(MarkLibAddRequest markLibAddRequest) {
		
		MarkLibEntity markLibEntity = new MarkLibEntity();
		
		BeanUtil.copyProperties(markLibAddRequest , markLibEntity , CopyOptions.create().ignoreNullValue());
		
		markLibMapper.insert(markLibEntity);
		
		return markLibEntity.getId();
	}

	/**
	 * @function 删除MarkLib
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public void deleteMarkLibById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		MarkLibEntity markLibEntity = markLibMapper.selectById(id);
		
		if(markLibEntity == null)
			throw new BusinessException("id不合法");
		
		UpdateWrapper<MarkLibEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", id);
		
		//因为注解了@TableLogic , 自动会把删除标志位设置为1 
		markLibMapper.delete(updateWrapper);
		
		//操作轨迹标题
		OperateTraceAspect.setTitle(markLibEntity.getLibName());
	}

	/**
	 * @function 修改MarkLib
	 * @param markLibUpdateRequest
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public void updateMarkLib(MarkLibUpdateRequest markLibUpdateRequest) {
		
		if(markLibUpdateRequest.getId() == null)
			throw new BusinessException("id不能为空");
		
		MarkLibEntity markLibEntity = markLibMapper.selectById(markLibUpdateRequest.getId());
		
		if(markLibEntity == null)
			throw new BusinessException("id不合法");
		
		BeanUtil.copyProperties(markLibUpdateRequest , markLibEntity , CopyOptions.create().ignoreNullValue());
		
		markLibMapper.updateById(markLibEntity);
		
	}

	/**
	 * @function 根据ID查询MarkLib Entity
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public MarkLibEntity findMarkLibEntityById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
			
		MarkLibEntity markLibEntity = markLibMapper.selectById(id);
		
		return markLibEntity;
	}
	
	/**
	 * @function 根据ID查询MarkLib DTO
	 * @param id
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public MarkLibDTO findMarkLibById(Long id) {
		
		if(id == null)
			throw new BusinessException("id不能为空");
	
		MarkLibEntity markLibEntity = markLibMapper.selectById(id);
		
		MarkLibDTO  markLibDTO = BeanUtil.toBean(markLibEntity , MarkLibDTO.class);
		
		return markLibDTO;
	}

	/**
	 * @function 根据MarkLibQueryVo查询MarkLib列表
	 * @param markLibQueryRequest
	 * @author 肖荣辉
	 * @date 2023-12-25 14:58:40
	*/
	public CommonResult<CommonPage<MarkLibDTO>> findMarkLibList(MarkLibQueryRequest markLibQueryRequest) {
		
		Page<MarkLibEntity> page = markLibQueryRequest.createPage();

		QueryWrapper<MarkLibEntity> queryWrapper = markLibQueryRequest.createQuery();
		
		markLibMapper.selectPage(page, queryWrapper);
		
		List<MarkLibDTO> dtoList = BeanUtil.copyToList(page.getRecords() , MarkLibDTO.class);
		
		return CommonResult.success(CommonPage.create(markLibQueryRequest.getPageNum() , markLibQueryRequest.getPageSize() , dtoList, page.getTotal()));
	}
	
	
	
}
